package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.auth.model.po.XcTeacher;
import com.xuecheng.auth.model.po.XcUser;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.feignclient.auth.XcUserClient;
import com.xuecheng.content.feignclient.auth.XcTeacherClient;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper,
        CourseTeacher> implements CourseTeacherService {
    @Autowired
    CourseBaseInfoService courseBaseInfoService;

    @Autowired
    XcTeacherClient teacherClient;

    @Autowired
    XcUserClient userClient;

    @Transactional
    @Override
    public CourseTeacher saveCourseTeacher(Long companyId, CourseTeacher courseTeacher) {
        if(courseTeacher.getCourseId() == null){
            XueChengPlusException.cast("课程id不能为空");
        }
        CourseBase courseBase = courseBaseInfoService.getById(courseTeacher.getCourseId());
        if(courseBase == null){
            XueChengPlusException.cast("当前课程不存在");
        }
        if(!courseBase.getCompanyId().equals(companyId)){
            XueChengPlusException.cast("当前课程不属于本机构，无权操作");
        }
        //根据判断当前教师是否已经存在，如果不存在则给该教师开通账号，并添加到教师表
        XcTeacher teacherQuery = new XcTeacher();
        teacherQuery.setNumber(courseTeacher.getTeacherNumber());
        XcTeacher teacher = teacherClient.getTeacher(teacherQuery);
        teacherQuery.setName(courseTeacher.getTeacherName());
        teacherQuery.setIntro(courseTeacher.getIntroduction());
        teacherQuery.setPic(courseTeacher.getPhotograph());
        if(courseTeacher.getId() == null){
            if(teacher == null){
                //教师不存在，开通账号并添加教师
                XcUser user = userClient.createAccount(companyId);
                user.setName(teacherQuery.getName());
                user.setUserpic(teacherQuery.getPic());
                userClient.saveUser(user);
                teacherQuery.setUserId(user.getId());
                teacherQuery = teacherClient.saveTeacher(teacherQuery);
            }else{
                //存在，更新教师
                teacherQuery.setId(teacher.getId());
                XcUser user = new XcUser();
                user.setId(teacher.getUserId());
                user.setName(teacher.getName());
                user.setUserpic(teacher.getPic());
                userClient.saveUser(user);
                teacherQuery.setUserId(user.getId());
                teacherQuery = teacherClient.saveTeacher(teacherQuery);
            }
            //新增
            courseTeacher.setTeacherId(teacherQuery.getId());
            save(courseTeacher);
        }else{
            //修改
            updateById(courseTeacher);
            //然后更新教师表中对应教师的相关信息
            teacherQuery.setId(teacher.getId());
            teacherClient.saveTeacher(teacherQuery);
            XcUser user = new XcUser();
            user.setId(teacher.getUserId());
            user.setName(teacher.getName());
            user.setUserpic(teacher.getPic());
            userClient.saveUser(user);
        }
        return getById(courseTeacher.getId());
    }

    /**
     * 师资通用校验，判断课程是否存在，判断当前操作者的所属机构和课程机构是否一致
     * @param courseId
     * @param companyId
     */
    public void checkCourseAndCompany(Long companyId,Long courseId){
        //先查询当前课程看看当前课程是否存在
        CourseBase courseBase = courseBaseInfoService.getById(courseId);
        if(courseBase == null){
            XueChengPlusException.cast("当前查询的课程不存在，没有对应的师资信息!");
        }
        //然后判断当前课程的所属机构和操作者的所属机构是否一致
        if(!courseBase.getCompanyId().equals(companyId)){
            XueChengPlusException.cast("你不是当前机构成员，无权查看!");
        }
    }
}
